import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/common/xindong_types.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/models/xindong_file_select_class.dart';
import 'package:xindong/routes/tab_moments/controller/xindong_dongtai_handle.dart';
import 'package:xindong/routes/tab_moments/publish/xindong_dongtai_publish_class.dart';
import 'package:xindong/routes/tab_moments/publish/xindong_dongtai_publish_failed_dialog.dart';
import 'package:xindong/utils/xindong_text_util.dart';

class XinDongDongTaiPublishController extends GetxController {
  static XinDongDongTaiPublishController get to => Get.find();

  final _momentHandle = XinDongDongTaiHandle();
  final textEditingController = TextEditingController();

  // 图片数组
  List<XinDongSelectFileClass>? imageFileClassList;

  // 视频
  XinDongSelectFileClass? videoFileClass;

  // 发布的模型
  XinDongDongTaiPublishClass publishClass = XinDongDongTaiPublishClass();

  // 请求发布
  void _requestPublish() async {
    showLoading();
    // 视频（封面+视频）
    if (videoFileClass != null && !TextUtil.isEmpty(videoFileClass?.videoPath)) {
      final coverUrl = await XinDongNetworking.instance.uploadFile(filePath: videoFileClass!.file.path);
      if (TextUtil.isEmpty(coverUrl)) {
        hideLoading();
        return;
      }
      final videoUrl = await XinDongNetworking.instance.uploadVideo(filePath: videoFileClass!.videoPath!);
      if (TextUtil.isEmpty(videoUrl)) {
        hideLoading();
        return;
      }
      publishClass.cover = coverUrl!;
      publishClass.video = videoUrl!;
    }
    // 图片，可多图
    else if (imageFileClassList != null && imageFileClassList!.isNotEmpty) {
      List<String> filePaths = imageFileClassList!.map((e) => e.file.path).toList();
      String? result = await XinDongNetworking.instance.uploadFiles(filePaths: filePaths);
      if (result == null) {
        hideLoading();
        return;
      }
      publishClass.masterimg = result;
    }
    // 调用请求
    bool result = await _momentHandle.requestPublish(model: publishClass);
    hideLoading();
    if (result == true) {
      textEditingController.clear();
      imageFileClassList = null;
      videoFileClass = null;
      publishClass = XinDongDongTaiPublishClass();
      // 获取BuildContext
      Get.back(result: true);
    }
  }

  // 请求打卡，不需要费用和条件
  void requestPunchTheClock() {
    _requestPublish();
  }

  // 检测发布权限
  void authorityEnablePublish() async {
    publishClass.content = textEditingController.text.trim();
    if (TextUtil.isEmpty(publishClass.content)) {
      showToast("请输入发布内容");
      return;
    }
    if (imageFileClassList == null && videoFileClass == null) {
      showToast("请选择图片或视频");
      return;
    }
    if (XinDongUserController.to.sex == Gender.female) {
      _requestPublish();
    } else {
      final object = await XinDongNetworking.instance.request(
        XinDongApi.momentCheckPublish,
        method: XinDongHttpMethod.post,
        parameters: {"type": 2},
      );
      if (object.code == XinDongServerStatus.success) {
        _requestPublish();
      } else if (object.code == 202) {
        XinDongDongTaiPublishFailedDialog.show(
          money: object.money,
          onTapPay: () {
            _requestPublish();
          },
        );
      }
    }
  }
}
